const app = getApp()
const date = new Date()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    request_common_para: "",
    id: "",
    bankListData: [], //银行列表
    followListStatus: [], //进件状态
    follow_status_index: 0,
    bookStatus: "",
    lookCarRemark: "",
    tradeCar: "",
    carVinNo: "",
    tradePrice: "",
    followTime: "---请选择---",
    followBank: "---请选择---",
    follow_bank_index: 0,
    followStatus: "---请选择---",
    signTime: "---请选择---",
    loanBank: "---请选择---",
    loan_bank_index: 0,
    loanPrice: "",
    remark: "",
    userManagerId: "",
    userManagerRecordId: "",
    strartdate: date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(),
    isDisabled:true,
    isLoanDisable:true,
    isLoanFromDisable:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    app.getBankListData()
    let follow_list_status = ["---请选择---", "审核中", "审核通过", "审核不通过"]
    this.setData({
      request_common_para: app.globalData.request_common_para,
      bankListData: app.globalData.bankListData,
      followListStatus: follow_list_status
    })
    let user_manager_record_id = options.id
    let data = app.globalData.request_common_para
    data.user_manager_record_id = user_manager_record_id
    wx.request({
      url: app.getHost() + "/datalake/v1/userManages/getBookRecordDetailsByUserManagerRecordId.json",
      data: data,
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: res => {
        if (res.data.return_code == "SUCCESS") {
          let bookRecordDetails = res.data.data.bookRecordDetails
          let follow_status_index = this.data.followListStatus.indexOf(bookRecordDetails.followStatus)
          let follow_bank_index = this.data.bankListData.indexOf(bookRecordDetails.followBank)
          let loan_bank_index = this.data.bankListData.indexOf(bookRecordDetails.loanBank)
          let signTime = bookRecordDetails.signTime ? bookRecordDetails.signTime : "---请选择---"
          let followTime = bookRecordDetails.followTime ? bookRecordDetails.followTime : "---请选择---"
          this.setData({
            id: bookRecordDetails.id,
            followTime: followTime,
            follow_status_index: follow_status_index == -1 ? 0 : follow_status_index,
            followStatus: bookRecordDetails.followStatus,
            bookStatus: bookRecordDetails.bookStatus,
            follow_bank_index: follow_bank_index == -1 ? 0 : follow_bank_index,
            followBank: bookRecordDetails.followBank,
            lookCarRemark: bookRecordDetails.lookCarRemark,
            tradeCar: bookRecordDetails.tradeCar,
            carVinNo: bookRecordDetails.carVinNo,
            tradePrice: bookRecordDetails.tradePrice,
            signTime: signTime,
            loan_bank_index: loan_bank_index == -1 ? 0 : loan_bank_index,
            loanBank: bookRecordDetails.loanBank,
            loanPrice: bookRecordDetails.loanPrice,
            remark: bookRecordDetails.remark,
            userManagerId: bookRecordDetails.userManagerId,
            userManagerRecordId: bookRecordDetails.userManagerRecordId,
            isDisabled: bookRecordDetails.bookStatus?true:false
          })
        } else {

        }
      },
      complete: res => {

      },
      fail: res => {

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  submitFromBookRecordDetails: function(e) {
    let data = this.data.request_common_para;
    let book_record_details = e.detail.value
    //{"value":{"look_car_remark":"","trade_car":"","car_vin_no":"","trade_price":"","loan_price":"","remark":""}
    let look_car_remark = book_record_details.look_car_remark //带看车辆
    data.lookCarRemark = look_car_remark
    let trade_car = book_record_details.trade_car //成交车辆
    data.tradeCar = trade_car
    let car_vin_no = book_record_details.car_vin_no //车辆vin
    data.carVinNo = car_vin_no
    let trade_price = book_record_details.trade_price //成交价
    data.tradePrice = trade_price
    let follow_time = this.data.followTime; //进件时间
    data.followTime = follow_time=="---请选择---" ? "" : follow_time
    let follow_bank = this.data.followBank //进件银行
    data.followBank = follow_bank == "---请选择---" ? "" : follow_bank
    let follow_status = this.data.followStatus //进价状态
    data.followStatus = follow_status == "---请选择---" ? "" : follow_status
    let sign_time = this.data.signTime //签约时间
    data.signTime = sign_time == "---请选择---"?"": sign_time
    let loan_bank = this.data.loanBank; //贷款银行
    data.loanBank = loan_bank == "---请选择---" ? "" : loan_bank
    let loan_price = book_record_details.loan_price //贷款金额
    data.loanPrice = loan_price
    let remark = book_record_details.remark //接待备注
    let bookStatus = this.data.bookStatus
    data.bookStatus = bookStatus
    data.remark = remark
    let id = this.data.id //book_record_details_id
    data.id = id

    if (!bookStatus) {
      app.noTitlemodalTap("到店结果未选中")
      return;
    }
    if (bookStatus == "10") {

    } else if (bookStatus == "20") {
      if (!look_car_remark) {
        app.noTitlemodalTap("带看车辆不能为空");
        return;
      }
    } else if (bookStatus == "30") {
      if (!trade_car) {
        app.noTitlemodalTap("成交车辆不能为空")
        return;
      } else if (!car_vin_no) {
        app.noTitlemodalTap("车辆VIN不能为空")
        return
      } else if (!trade_price) {
        app.noTitlemodalTap("成交价不能为空")
        return
      }
    }

    wx.request({
      url: app.getHost() + '/datalake/v1/userManages/saveOrUpdateBookRecordDetials.json',
      data: data,
      method: 'POST',
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: res => {
        if (res.data.return_code == "SUCCESS") {
          app.toast4Tap(res.data.return_msg);

          var pages = getCurrentPages();
          var currPage = pages[pages.length - 1];   //当前页面
          var prevPage = pages[pages.length - 3];  //上一个页面
          console.info(prevPage)
          wx.redirectTo({
            url: "/"+prevPage.route
          })
        }
      },
      fail: res => {

      },
      complete: res => {

      }
    })

  },
  bookStatusRadioChange: function(e) {

    this.setData({
      bookStatus: e.detail.value
    })
    if (e.detail.value == "10") {
      this.setData({
        bookStatus: e.detail.value,
        follow_status_index: 0,
        lookCarRemark: "",
        tradeCar: "",
        carVinNo: "",
        tradePrice: "",
        followTime: "---请选择---",
        followBank: "---请选择---",
        follow_bank_index: 0,
        followStatus: "---请选择---",
        signTime: "---请选择---",
        loanBank: "---请选择---",
        loan_bank_index: 0,
        loanPrice: "",
        remark: ""
      })
    } else if (e.detail.value == "20") {
      this.setData({
        bookStatus: e.detail.value,
        follow_status_index: 0,
        tradeCar: "",
        carVinNo: "",
        tradePrice: "",
        followTime: "---请选择---",
        followBank: "---请选择---",
        follow_bank_index: 0,
        followStatus: "---请选择---",
        signTime: "---请选择---",
        loanBank: "---请选择---",
        loan_bank_index: 0,
        loanPrice: "",
        remark: ""
      })
    } else if (e.detail.value == "30") {
      this.setData({
        lookCarRemark: ""
      })
    }


  },
  bindFollowTimeChange: function(e) {
    this.setData({
      followTime: e.detail.value
    })
  },
  bindCancelFollowTime: function(e) {
    this.setData({
      followTime: '---请选择---'
    })
  },
  bindSignTimeChange: function(e) {
    this.setData({
      signTime: e.detail.value
    })
  },
  bindCancelSignTime: function(e) {
    this.setData({
      signTime: '---请选择---'
    })
  },
  bindPickerFollowBankChange: function(e) {
    this.setData({
      follow_bank_index: e.detail.value,
      followBank: this.data.bankListData[e.detail.value]
    })
  },
  bindPickerLoanBankChange: function(e) {
    this.setData({
      loan_bank_index: e.detail.value,
      loanBank: this.data.bankListData[e.detail.value]
    })
  },
  bindPickerFollowStatusChange: function(e) {
    this.setData({
      follow_status_index: e.detail.value,
      followStatus: this.data.followListStatus[e.detail.value]
    })
    
    if(this.data.followListStatus[e.detail.value]=="审核通过"){
      this.setData({isLoanFromDisable:false})
    }else{
      this.setData({ isLoanFromDisable: true, signTime: "---请选择---", loan_bank_index: 0, loanPrice:""})
    }
  }, 
  btnSave: function (e) {
    this.setData({
      isDisabled: false
    })
  },
  loanbindtap:function(e){
    if(this.data.isLoanDisable){
      this.setData({isLoanDisable:false})
    }else{
      this.setData({ isLoanDisable: true, followTime: "---请选择---", follow_bank_index: 0, follow_status_index:0})
    }
  }
})